#!/bin/sh

mkdir -p -m 750 $SNAP_DATA/apache/logs

# Make sure Nextcloud is installed and running
echo "Making sure nextcloud is setup..."
setup_nextcloud
if [ $? -ne 0 ]; then
	echo "Failed to setup nextcloud"
        exit 1
fi

echo "All set! Running httpd..."

. $SNAP/utilities/https-utilities

# Rewrite live cert symlinks that aren't using the current symlink.
# FIXME: Remove this migration once epochs and upgrade hooks are available.
if certificates_are_active; then
	live_basename="$(basename $(realpath $LIVE_CERTS_DIRECTORY))"
	self_signed_basename="$(basename $SELF_SIGNED_DIRECTORY)"
	custom_basename="$(basename $CUSTOM_DIRECTORY)"
	if [ "$live_basename" = "$self_signed_basename" ]; then
		activate_self_signed_certificate
	elif [ "$live_basename" = "$custom_basename" ]; then
		activate_custom_certificate
	else
		activate_certbot_certificate
	fi
fi

httpd-wrapper $@
